package no.awerup.armory_miner.collectors;

import java.io.IOException;
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import no.awerup.armory_miner.AMClient;

import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.ProducerTemplate;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import domain.Char;
import com.thoughtworks.xstream.XStream;

public class GuildCollector extends Collector {

	
	public void process(Exchange exchange) throws Exception {
		String guildName = "Crit for brains";//(String) exchange.getIn().getBody();
		String serverName = "Azuremyst";
		
		
		AMClient amClient = createAMClient();    
		System.out.println( "Getting guild info for '" + guildName + "' of '" + serverName + "'" );
    	String gs = amClient.getGuild( serverName, guildName );
    	System.out.println( gs );
    	mapGuild(gs);

	}

	private void mapGuild(String guildXML) throws ParserConfigurationException, SAXException, IOException,
			XPathExpressionException {

		DocumentBuilder builder = createDocumentBuilder();
		Document doc = builder.parse(new InputSource(new StringReader(guildXML)));

		XPathFactory factory = XPathFactory.newInstance();
		XPath xpath = factory.newXPath();
		
		javax.xml.xpath.XPathExpression guildNameXpath = xpath.compile("//guildInfo/guild/members/character/@name");
		javax.xml.xpath.XPathExpression characterNamesXpath = xpath.compile("//guildInfo/guild/members/character/@name");
		javax.xml.xpath.XPathExpression realmNameXpath = xpath.compile("//guildInfo/guildHeader/@realm");
		
		javax.xml.xpath.XPathExpression charactersXpath = xpath.compile("//guildInfo/guild/members/character");
		javax.xml.xpath.XPathExpression nameXpath = xpath.compile("@name");
		javax.xml.xpath.XPathExpression levelXpath = xpath.compile("@level");

		NodeList charactersNodeList = (NodeList) charactersXpath.evaluate(doc, XPathConstants.NODESET);

		ProducerTemplate template = camelContext.createProducerTemplate();

		for (int i = 0; i < charactersNodeList.getLength(); i++) {
			Node charNode = charactersNodeList.item(i);
			Char cha = new Char();

			cha.setName((String) nameXpath.evaluate(charactersNodeList.item(i), XPathConstants.STRING));
			cha.setLevel(((Double) levelXpath.evaluate(charactersNodeList.item(i), XPathConstants.NUMBER)).intValue());
			cha.setServerName((String) realmNameXpath.evaluate(charactersNodeList.item(i), XPathConstants.STRING));
			
			// cha.setName((String)
			// nameXpath.evaluate(charactersNodeList.item(i),
			// XPathConstants.STRING));
			// cha.setName((String)
			// nameXpath.evaluate(charactersNodeList.item(i),
			// XPathConstants.STRING));
			
			
			System.out.println( xstream.toXML(cha) );
		
			
			template.sendBody("amq:queue:test.queue", xstream.toXML(cha) );
//			template.sendBody("amq:queue:test.queue", cha );
			System.out.println(cha.getName());
			System.out.println(cha.getLevel());

		}
	}
}

